<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>论文检索系统</title>

    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
    <link rel="stylesheet" th:href="@{/css/mainstyle.css}">
</head>

<body>
    <div th:replace="common :: commonheader"></div>

    <div th:replace="common :: leftmenu"></div>

    <div class="right-content col-md-10">
        <form th:action="@{/admin/modifyAdminPwd}" method="post">
            <br/>
            <table class="table table-bordered">
                <tbody>
                <tr>
                    <td>用户名：</td>
                    <td><input type="text" name="name" th:value="${session.loginName}"></td>
                </tr>
                <tr>
                    <td>原密码：</td>
                    <td><input id="oldPwd" type="password" name="oldPwd"></td>
                </tr>
                <tr>
                    <td>新密码：</td>
                    <td><input id="newPwd" type="password" name="newPwd"></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <button type="submit" onclick="return modifyPwd()">提交</button>
                        <input type="button" value="返回" onclick="history.back()">
                    </td>
                </tr>
                </tbody>
            </table>
        </form>
    </div>
</body>

<script>
    document.getElementById("li_head3").className = "active";

    document.getElementById("a_modifyPwd").style.backgroundColor = "#4CAF50";
    document.getElementById("a_modifyPwd").style.color = "white";

    function modifyPwd() {
        let pwd = "[[${pwd}]]";
        let name = document.getElementById("name").value;
        let oldPwd = document.getElementById("oldPwd").value;
        let newPwd = document.getElementById("newPwd").value;

        jQuery.ajax({
            url: "/admin/getPassword",
            type: "post",
            async : false,
            data: {
                "name": name,
                "pwd": oldPwd
            },
            dataType: "JSON",
            success: function(data) {
                oldPwd = data;
            },
            error: function(errorMsg) {
                console.log(errorMsg);
            }
        });

        if (pwd !== oldPwd) {
            alert("原密码错误！");
            return false;
        }
        if (oldPwd === newPwd) {
            alert("新密码不能和原密码相同！");
            return false;
        }
        alert("密码修改成功！");
        return true;
    }
</script>

</html>